package org.knot.mockejb3.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.naming.NamingException;

import org.knot.mockejb3.EjbBeanAccess;


/**
 * 
 * 针对Annotation处理的管理类
 * @author yuxiaowei
 *
 */
public class AnnotationRuleManager {
    
    private static AnnotationRuleManager me;
    
    public static AnnotationRuleManager getInstance() throws NamingException {
        if (null == me) {
            me = new AnnotationRuleManager();
        }
        return me;
    }
    
    //防止有状态的SessionBean 环状 依赖创建实例；
    private Map<String, Set<EjbBeanAccess>>   stateFulBeanMap =  new HashMap<String, Set<EjbBeanAccess>>();
    
    // 无状态的sessionBean String：jndiname
    private   Map<String, EjbBeanAccess> stateLessBeanMap = new HashMap<String, EjbBeanAccess>();

    //无状态和有状态的都保存；
    private Map<EjbBeanAccess, ClassAnnotation> classeAnnotations = new HashMap<EjbBeanAccess, ClassAnnotation>();


    public  void createClassAnnotation(EjbBeanAccess sessionBean, String key) throws InstantiationException, IllegalAccessException {
        Class clazz = sessionBean.getBean().getClass();
        ClassAnnotation classAnnotation = new ClassAnnotation();
        //类
        for (Annotation annotation : clazz.getAnnotations()) {
           if ( AnnotationType.getAnnotationKeys().contains(annotation.annotationType().getName()))
           {
               RuleParams params = new RuleParams(0);
               params.setKey(key);
               params.setClazz(clazz);
               params.setSessionBean(sessionBean);
               classAnnotation.getAnnotations().put((AnnotationRule) AnnotationType.getAnnotations().get(annotation.annotationType().getName()).newInstance(), params);
           }
        }
        // 方法
        for (Method method : clazz.getDeclaredMethods()) {
            for (Annotation annotation : method.getAnnotations()) {
                if ( AnnotationType.getAnnotationKeys().contains(annotation.annotationType().getName()))
                {
                    RuleParams params = new RuleParams(1);
                    params.setKey(key);
                    params.setMethod(method);
                    params.setSessionBean(sessionBean);
                    classAnnotation.getAnnotations().put((AnnotationRule) AnnotationType.getAnnotations().get(annotation.annotationType().getName()).newInstance(), params);
                } 
            }
        }
        // 字段
        for (Field field : clazz.getDeclaredFields()) {
            for (Annotation annotation : field.getAnnotations()) {
                if ( AnnotationType.getAnnotationKeys().contains(annotation.annotationType().getName()))
                {
                    RuleParams params = new RuleParams(2);
                    params.setKey(key);
                    params.setField(field);
                    params.setSessionBean(sessionBean);
                    classAnnotation.getAnnotations().put((AnnotationRule) AnnotationType.getAnnotations().get(annotation.annotationType().getName()).newInstance(), params);
                } 
            }
        }
        classeAnnotations.put(sessionBean, classAnnotation);
    }

    public   Map<String, EjbBeanAccess> getStateLessBeanMap() {
        return stateLessBeanMap;
    }

    public Map<EjbBeanAccess, ClassAnnotation> getClasseAnnotations() {
        return classeAnnotations;
    }

    public Map<String, Set<EjbBeanAccess>> getStateFulBeanMap() {
        return stateFulBeanMap;
    }

}
